<template>
    <div class="login-container">
        <!--这里必须使用一个双标签-->
        <div id="login_box" >
            <div class="img_position">
                <el-avatar :size="140" :src="imgUrl"></el-avatar>
            </div>
            <el-card class="box-card">
                <el-form :model="ruleForm" status-icon :rules="rules" ref="ruleForm" label-width="100px" class="demo-ruleForm">
                    <el-form-item label="用户名" prop="username">
                        <el-input type="text" v-model="ruleForm.username" autocomplete="off"></el-input>
                    </el-form-item>
                    <el-form-item label="密码" prop="password">
                        <el-input type="password" v-model="ruleForm.password" autocomplete="off"></el-input>
                    </el-form-item>
                    <el-form-item>
                        <el-button type="primary"
                                   size="mini"
                                   :plain="true"
                                   @click="login"
                                   style="margin-left: 100px;width: 100px">登录</el-button>
                    </el-form-item>
                </el-form>
            </el-card>
        </div>
    </div>
</template>

<script>
    export default {
        name: "Login",
        data(){
            return {
                ruleForm: {
                    username: '',
                    password: ''
                },
                rules: {
                    name: [
                        {required: true, message:'用户名不能为空', trigger: 'blur'},
                    ],
                    password: [
                        {required: true, message: '密码不能为空', trigger: 'blur'},
                    ]
                },
                imgUrl:'https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fc-ssl.duitang.com%2Fuploads%2Fblog%2F202010%2F16%2F20201016234725_52ed2.jpg&refer=http%3A%2F%2Fc-ssl.duitang.com&app=2002&size=f9999,10000&q=a80&n=0&g=0n&fmt=auto?sec=1662528624&t=c2ac363744d360cd4d61b7a9ac9cffef'
            }
        },

        methods:{
            login(){
                //表单校验
                this.$refs['ruleForm'].validate((valid) => {
                    if(valid){
                        //url:后端登录接口的路径
                        this.$http.post("/sso/login",this.ruleForm).then(result=>{
                            if(result.data.code===2000){
                                //获取登录成功后的token
                                var token = result.data.data;
                                //把token保存 sessionStorage理解为session
                                console.log(result)
                                sessionStorage.setItem("token",token)
                                //路由跳转
                                this.$router.push("/home")
                                // //外网跳转
                                // window.location.href="";
                            }else{
                                this.$message.error(result.data.msg)
                            }
                        });
                    }
                })
            }
        }
    }
</script>

<style>

    body{
        margin: 0;
        padding: 0;
    }
    #box{
        height: 100%;
        background: url("../assets/logo.png");
    }
    #login_box{
        position: relative;
        width: 500px;
        margin: 250px auto;
    }
    #login_box div.img_position{
        position: absolute;
        left: 200px;
        top: -70px;
    }
    .login-container{
        width: 100%;
        height: 100%;
        background-image: url("../assets/R-C.jpg");
        background-size: cover;
        background-position: center;
        position: fixed;
    }

    .text {
        font-size: 14px;
    }

    .item {
        padding: 18px 0;
    }

    .box-card {
        padding: 100px 50px 0 0;
        width: 480px;
    }

</style>